using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace ru.yourpictures.Core.Imaging.Filters.PhotoFilters
{
  /// <summary>
  /// Rounded Corners filter class .  Turns right 90 degrees corners to round corners
    /// Reference: http://www.codeproject.com/KB/cs/yael_image_filters.aspx
    /// </summary>
    [FilterImplementation(GdiPlusImplemented = true, LowLevelImplemented = false)]
    public class RoundedCorners : FilterBase
  {
        #region Private Fields
        private float _cornerRadius = 50;
        #endregion Private Fields
        
        #region Filter Properties
        /// <summary>
        /// Determins the corner's radius. in pixels
        /// </summary>
        public float CornerRadius
        {
            get { return _cornerRadius; }
            set
            {
                if (value > 0)
                    _cornerRadius = value;
                else
                    _cornerRadius = 0;
            }
        }
        #endregion Filter Properties

        #region Public Filter Methods
        /// <summary>
        /// Executes this curved corners 
        /// filter on the input image and returns the result
        /// Make sure you set the BackGroundColor property before running this filter.
        /// </summary>
        /// <param name="inputImage">input image</param>
        /// <returns>Curved Corner Image</returns>
        /// <example>
        /// <code>
        /// Image transformed;
        /// RoundedCorners rounded = new RoundedCorners();
        /// rounded.BackGroundColor = Color.FromArgb(255, 255, 255, 255);
        /// rounded.CornerRadius = 15;
        /// transformed = rounded.ExecuteFilter(myImg);
        /// </code>
        /// </example>
        public override Bitmap Convert(Bitmap b)
        {
            Bitmap b2 = b.Clone() as Bitmap;
            Apply(b2);
            return b2;
        }

        public override void Apply(Bitmap b)
        {
            _b = b;
            _width = b.Width;
            _height = b.Height;

            Bitmap bm = new Bitmap(_width, _height);
            //Bitmap bm = new Bitmap(_width, _height, PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(bm);
            g.DrawImage(b, 0, 0, _width, _height);
            Brush backGroundBrush = new SolidBrush(BackGroundColor);

            //Top left
            GraphicsPath gp = new GraphicsPath();
            float radius = _cornerRadius;
            gp.AddLine(0, 0, radius / 2, 0);
            gp.AddLine(0, 0, 0, radius / 2);
            gp.AddArc(0, 0, radius, radius, 180, 90);
            g.FillPath(backGroundBrush, gp);

            //Top Right
            gp = new GraphicsPath();
            gp.AddLine(_width - radius / 2, 0, _width, 0);
            gp.AddLine(_width, 0, _width, radius / 2);
            gp.AddArc(_width - radius, 0, radius, radius, 270, 90);
            g.FillPath(backGroundBrush, gp);

            //Bottom Left
            gp = new GraphicsPath();
            gp.AddLine(0, b.Height - radius / 2, 0, _height);
            gp.AddLine(0, b.Height, radius / 2, _height);
            gp.AddArc(0, b.Height - radius, radius, radius, 90, 90);
            g.FillPath(backGroundBrush, gp);

            //Bottom Right
            gp = new GraphicsPath();
            gp.AddLine(_width - radius / 2, b.Height, _width, _height);
            gp.AddLine(_width, _height - radius / 2, _width, _height);
            gp.AddArc(_width - radius, b.Height - radius, radius, radius, 0, 90);
            g.FillPath(backGroundBrush, gp);
        }
        #endregion Public Filter Methods

        protected override void FilterImplementation()
        {
            throw new NotImplementedException("Low level mode is not implemented");
        }
  }
}
